package com.fgr.controller;

import com.fgr.beans.Animal;
import com.fgr.beans.AnimalType;
import com.fgr.service.AnimalService;
import com.fgr.service.AnimalTypeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

import java.util.List;

import java.util.UUID;

@Controller
public class AnimalController {
    @Autowired
    AnimalService animalService;

    @Autowired
    AnimalTypeService animalTypeService;

    @ResponseBody
    @GetMapping("findAllAnimal")
    public void findAllAnimal(HttpSession session){
        List<Animal> animals = animalService.selectAllAnimal();
        session.setAttribute("animals",animals);

//        return "redirect:allAnimalType";
    }

    @GetMapping("findAllAnimalBg")
    public String findAllAnimalBg(Integer pageNum,Model model){
//        if (pageNum==null){
//            pageNum=1;
//        }
//        Page<Object> page = PageHelper.startPage(pageNum, 1);
        List<Animal> animals = animalService.selectAllAnimal();
//        System.out.println(page);

        model.addAttribute("animals",animals);
        model.addAttribute("tda",0);
        return "background/animal";
    }
    @ResponseBody
    @RequestMapping("findAnimalByTypeId")
    public void findAnimalByTypeId(@RequestParam("type_id") Integer type_id, HttpSession session){
        List<Animal> animals = animalService.selectAnimalByTypeIdAndStat(type_id,0);
        session.setAttribute("animals",animals);
        System.out.println(animals);
    }

    @GetMapping("findAnimalByTypeIdBg")
    public String findAnimalByTypeIdBg(@RequestParam("typeId")Integer typeId,Integer tyParentId,Model model){
        if (tyParentId==0){
            //从父类型页面跳子类型页面
            List<AnimalType> animalTypes = animalTypeService.selectAllAnimalTypeByParentType(typeId);
            model.addAttribute("allAnimalType",animalTypes);
            model.addAttribute("t",typeId);
            return "background/animal-species";
        }else {
            //从子类型页面跳动物页面
            List<Animal> animals = animalService.selectAnimalByTypeId(typeId);
            model.addAttribute("animals",animals);
            model.addAttribute("tda",typeId);
            return "background/animal";
        }
    }

    @GetMapping("findAnimalById")
    public String findAnimalById(Integer id, Model model){
        Animal animal = animalService.selectAnimalById(id);
        model.addAttribute("animal",animal);
        return "foreground/adopt-button";

    }
    @ResponseBody
    @PostMapping("deleteAnimalByIdBg")
    public String deleteAnimalByIdBg(@RequestParam("an_id") Integer an_id){
        String s = animalService.deleteAdapter(an_id);
        return s;
    }

    @ResponseBody
    @PostMapping("addAnimal")
    public String insertAnimal(Animal animal, @RequestParam(value = "Img",required = false)MultipartFile Img) throws IOException {

        System.out.println("---------------------"+Img+"==========="+animal);
        //1.获取上传文件对象
        String filename = Img.getOriginalFilename();
        //2.解决文件重名问题
        String substring = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID().toString() + substring;

        //3.获取服务器上的文件的路径
        //去服务器里面找到picture的图片路径
        String picturePath =new File("").getAbsolutePath() + File.separator + ".." + File.separator + "webapps" + File.separator + "picture";
        //输出路径
        System.out.println(picturePath);
        File file = new File(picturePath);
        //exists()检测文件是否存在
        if (!file.exists()) {
            //如果不存在创建文件
            file.mkdirs();
        }
        //拼接真正的文件路径
        //File.separator为获取路径中间的拼接符，windows中为//
        String realPath = picturePath + File.separator + filename;

        Img.transferTo(new File(realPath));
        //网络路径
        String path="http://116.204.113.19:8080/picture/"+filename;

        animal.setAnPicture(path);

        String s = animalService.insertAdapter(animal);

        return s;

    }

    @ResponseBody
    @PostMapping("modifyAnimal")
    public String updateAnimal(Animal animal) throws IOException {
        String s = animalService.updateAnimal(animal);
        return s;

    }

}
